package com.hugo.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hugo.commonutiles.R;
import com.hugo.eduservice.entity.EduTeacher;
import com.hugo.eduservice.entity.vo.TeacherQuery;
import com.hugo.eduservice.service.EduTeacherService;
import com.hugo.serviceconfig.exceptionhandler.HugoException;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author hugo
 * @since 2020-08-28
 */
@Api()
@RestController
@RequestMapping("/eduservice/edu_teacher")
@CrossOrigin
public class EduTeacherController {
    @Autowired
    private EduTeacherService teacherService;

    //1.查找所有教师
    @GetMapping("findall")
    public R findAllTeacher(){
        List<EduTeacher> list = teacherService.list(null);

//        try{
//            int i = 10 / 0;
//        }catch (Exception e) {
//            throw new HugoException(01,"执行了自定义异常");
//        }

        return R.ok().data("items",list);


    }

    //2.删除某个id
    @DeleteMapping("{id}")
    public R removeTeacher(@PathVariable String id){

        boolean flag = teacherService.removeById(id);
        if (flag){
           return R.ok();
        }else {
            return R.error();
        }

    }

    //3.分页查询讲师方法
    //current 查询当前页  以及个数
    @GetMapping("pageTeacher/{current}/{limit}")
    public R pageTeacher(@PathVariable long current
    ,@PathVariable long limit){
        Page<EduTeacher> teacherPage=new Page<>(current,limit);
        //查询分页的数据
        teacherService.page(teacherPage,null);
        //查询总数
        long total=teacherPage.getTotal();
        //获取数据
        List<EduTeacher> records = teacherPage.getRecords();
        return R.ok().data("total",total).data("rows",records);
    }

    //4.分页查询带条件
    @PostMapping("pageTeacherQuery/{current}/{limit}")
    public R pageTeacherCondition(@PathVariable long current
            , @PathVariable long limit, @RequestBody(required = false) TeacherQuery teacherQuery){

        //创建page对象
        Page<EduTeacher> pageteacher=new Page<>(current,limit);
        //构造条件
        QueryWrapper<EduTeacher> queryWrapper=new QueryWrapper<>();
        //多条件组合查询
        //判断条件值是否为空
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();
        //判断是否为空 不为空则拼接 //传的是字段名称
        if(!StringUtils.isEmpty(name)){
            queryWrapper.like("name",name);
        }
        if(!StringUtils.isEmpty(level)){
            queryWrapper.eq("level",level);
        }
        if(!StringUtils.isEmpty(begin)){
            queryWrapper.ge("gmt_create",begin);
        }
        if(!StringUtils.isEmpty(end)){
            queryWrapper.le("gmt_modified",end);
        }
        queryWrapper.orderByDesc("gmt_modified");

        //调用方法
        teacherService.page(pageteacher,queryWrapper);

        long total=pageteacher.getTotal();//总数据数
        List<EduTeacher> records = pageteacher.getRecords();//数据list集合
        return R.ok().data("total",total).data("rows",records);

    }

    //5.添加讲师
    @PostMapping("addTeacher")
    public R addTeacher(@RequestBody EduTeacher eduTeacher){

        boolean save=teacherService.save(eduTeacher);
        if (save){
            return R.ok();
        }else {
            return R.error();
        }
    }

    //6.根据讲师id修改
    @GetMapping("getTeacher/{id}")
    public R getTeacher(@PathVariable String id){
        EduTeacher eduTeacher = teacherService.getById(id);
        return R.ok().data("teacher",eduTeacher);
    }

    //7.讲师修改
    @PostMapping("updateTeacher")
    public R updateTeacher(@RequestBody EduTeacher eduTeacher){
        boolean save=teacherService.updateById(eduTeacher);
        if (save){
            return R.ok();
        }else {
            return R.error();
        }
    }



}

